35 Lecture

CS201

Midterm & Final Term Short Notes

Streams

In C++, streams are used to perform input and output operations on files, standard input/output devices (such as the console), and other sources/destinations of data. Streams allow for easy reading and writing of data in a variety of formats, in


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. Which header file is used for handling input/output operations in C++? A) iostream B) string C) vector D) algorithm Answer: A

  2. Which of the following is not a standard stream in C++? A) cin B) cout C) cerr D) clog Answer: D

  3. Which operator is used for insertion (output) operation with streams? A) << B) >> C) || D) && Answer: A

  4. Which operator is used for extraction (input) operation with streams? A) << B) >> C) || D) && Answer: B

  5. What is the default mode for file stream in C++? A) read B) write C) append D) binary Answer: A

  6. Which stream is used for handling errors during input/output operations? A) cin B) cout C) cerr D) clog Answer: C

  7. Which function is used to open a file for reading in C++? A) open() B) read() C) write() D) close() Answer: A

  8. Which function is used to close a file in C++? A) open() B) read() C) write() D) close() Answer: D

  9. Which function is used to check if a file is open or not in C++? A) is_open() B) read() C) write() D) close() Answer: A

  10. Which function is used to read a line of text from a file in C++? A) getline() B) read() C) write() D) close() Answer: A



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a stream in C++? Answer: A stream is an abstraction that represents a sequence of data flowing between a program and an input/output device.

  2. What are the three types of streams in C++? Answer: The three types of streams in C++ are input streams, output streams, and error streams.

  3. What is the purpose of using stream manipulators in C++? Answer: Stream manipulators are used to modify the output formatting of streams, such as setting the width or precision of output data.

  4. What is the difference between text mode and binary mode when opening a file stream in C++? Answer: Text mode is used for reading and writing text files, while binary mode is used for reading and writing binary files.

  5. What is the difference between cin and getline() in C++? Answer: cin is used to read input data from the console, while getline() is used to read a line of input data from a file.

  6. How can you open a file for writing in C++? Answer: You can open a file for writing in C++ by calling the open() function with the mode parameter set to "out" or "out | trunc".

  7. What is the purpose of the flush() function in C++? Answer: The flush() function is used to clear the output buffer and ensure that any pending output data is written to the output device.

  8. How can you check if an input operation has failed in C++? Answer: You can check if an input operation has failed by calling the fail() function on the input stream.

  9. How can you read data from a stringstream in C++? Answer: You can read data from a stringstream in C++ by calling the str() function to get the stream's internal string buffer, and then using standard string operations to extract the data.

  10. How can you write data to a file in binary mode in C++? Answer: You can write data to a file in binary mode in C++ by opening the file stream with the mode parameter set to "out | binary", and then using the write() function to write data in binary format.

In C++, streams are used to handle input and output operations on various data sources, including files and standard input/output devices such as the console. The three types of streams in C++ are input streams, output streams, and error streams. Input streams are used to read data from a source, while output streams are used to write data to a destination. Error streams are used to handle errors and exceptions during input/output operations. Streams can be connected to various types of sources and destinations, including files, memory buffers, and network sockets. The standard input/output streams (cin, cout, and cerr) are connected to the console by default, allowing for easy input and output of data. Stream manipulators are used to modify the formatting of output data, such as setting the width or precision of numeric output. Stream flags are used to control the behavior of input and output operations, such as the skipws flag which controls whether or not whitespace is skipped when reading input data. File streams in C++ can be opened in either text mode or binary mode. Text mode is used for reading and writing text files, while binary mode is used for reading and writing binary files. Streams can also be used with stringstream objects, which allow for easy manipulation of strings as if they were streams. stringstream objects can be used to convert between different types of data, such as converting a numeric value to a string. Overall, streams provide a powerful and flexible way to handle input and output operations in C++, allowing for easy manipulation of data from a variety of sources and in a variety of formats.